package JASYPT;

import org.apache.commons.codec.binary.Hex;
import org.jasypt.util.digest.Digester;

import java.util.Arrays;

public class DigesterDemo {

    public static void main(String[] args) {
        //
        // Creates a new instance of Digester, using the SHA-1 algorithm.
        //
        Digester digister = new Digester("SHA-1");

        byte[] message = "Hello World from Jasypt".getBytes();

        //
        // Creates a disgest from a array of byte message.
        //
        byte[] digest = digister.digest(message);

        System.out.println("Digest = " + new String(digest));
        System.out.println("Digest = " + Arrays.toString(digest));

        System.out.println("Digest = " + toHexString(digest));
        System.out.println("Digest = " + new String(Hex.encodeHex(digest)));
    }

    private static final byte[] Hexhars = {
    	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    	'A', 'B', 'C', 'D', 'E', 'F'
    };

	public static String toHexString( byte[] bytes ) {

		StringBuilder s = new StringBuilder( 2 * bytes.length );

		for( int i = 0 ; i < bytes.length ; i++ ) {
			int v = bytes[i] & 0xff;
			s.append( (char) Hexhars[v >> 4] );
			s.append( (char) Hexhars[v & 0xf] );
		}

		return s.toString();
	}
}
